﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FourthWallProject.GameObjects.Solids;

namespace FourthWallProject.GameObjects
{
    class Character : PhysicsEnabledGameObject 
    {
        public Character(Texture2D texture, Rectangle rectangle,int speedLimit, Game game)
            :base(texture,rectangle,Color.White, game)
        {
            _speedLimit = speedLimit;
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu2"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu3"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu4"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu5"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu6"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu7"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu8"));
            textures.Add(game.Content.Load<Texture2D>("Images/Character/pingu9"));
            this.Visible = true;
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
                if(CurrentSpeedX > 0)
                    ScreenManager.SpriteBatch.Draw(GetTexture(), this.RectangleWithCameraOffset, Color);
                else if(CurrentSpeedX < 0)
                    ScreenManager.SpriteBatch.Draw(GetTexture(), this.RectangleWithCameraOffset, null,Color,0,Vector2.Zero,SpriteEffects.FlipHorizontally,0);
                else
                    ScreenManager.SpriteBatch.Draw(GetTexture(), this.RectangleWithCameraOffset, Color);

            ScreenManager.SpriteBatch.End();
        }

        int counter = 0;
        public Texture2D GetTexture()
        {
            counter++;
            if (counter > 10)
            {
                counter = 0;
                if (currentTexture < textures.Count - 1)
                {
                    if (CurrentSpeedX > 0)
                        currentTexture++;
                    else if (CurrentSpeedX < 0 && currentTexture > 0)
                        currentTexture--;
                    else if (CurrentSpeedX < 0 && currentTexture == 0)
                        currentTexture = textures.Count - 1;
                }
                else
                    currentTexture = 0;
            }
            return textures[currentTexture];
        }
        List<Texture2D> textures = new List<Texture2D>();

        int _speedLimit;
        private int currentTexture;
        public Vector2 Speed { get; set; }

        public Vector2 SpeedLimitMax
        {
            get { return new Vector2(_speedLimit, _speedLimit); }
        }
        
        public Vector2 SpeedLimitMin
        {
            get { return new Vector2(SpeedLimitMax.X * -1, SpeedLimitMax.Y * -1); }
        }
    }
}

